package com.alit.face.koala.service.api;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alit.face.koala.app.data.AppResponse;
import com.alit.face.koala.app.data.OnlineData;
import com.alit.face.koala.app.data.Page;
import com.alit.face.koala.app.data.Translations;
import com.litong.utils.projectvariable.PVU;

@Service
public class OnlineService {

  private List<Translations> translationsList = null;

  public AppResponse lang() {
    AppResponse retval = new AppResponse();
    retval.setCode(0);
    retval.setPage(new Page());
    OnlineData onlineData = new OnlineData();
    onlineData.setDirection("left");
    onlineData.setLang("中文简体");
    onlineData.setLang_code("zh-Hans");
    onlineData.setTranslations(getTranslations());
    onlineData.setUpdate_time(1566965291);
    retval.setData(onlineData);
    return retval;
  }

  private List<Translations> getTranslations() {
    if (translationsList == null) {
      String filename = "translations.json";
      File file = new File(PVU.getJsonFilePath(filename));
      String string = null;
      try {
        string = FileUtils.readFileToString(file, "utf-8");
      } catch (IOException e) {
        e.printStackTrace();
      }
      // string中的中文虽然是unicode编码,但是在转换时会自动转换为中文
      translationsList = JSON.parseArray(string, Translations.class);
    }
    return translationsList;

  }
}
